package hu.myai.api.triggers;

import hu.myai.model.TriggerVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bozbalint
 * 
 */
abstract public class AbstractTriggerConsumer implements TriggerConsumerInterface {

	/**
	 * Name of the consumer
	 */
	protected String name;

	/**
	 * Collection of the names of necessary consumers
	 */
	protected List<String> triggerNameList = new ArrayList<String>();


	public AbstractTriggerConsumer(String name){
		this.name = name;
	}
	/**
	 * for pushing trigger value. It invokes strategy calculation on every
	 * trigger event.
	 * 
	 * @param name
	 * @param value
	 */
	@Override
	public void addTrigger(String name, TriggerVO trigger) {

		startCalculation(name, trigger);
	}

	/**
	 * strategy specific calculation. It returns null if no trade event is
	 * necessary. It can use triggerMap, triggerNameList
	 */
	abstract protected void startCalculation(String name, TriggerVO value);

	/**
	 * @return the value of name
	 */
	public String getName() {
		return name;
	}

	/**
	 * When an strategy registers itself in an trigger than the trigger
	 * registers itself using this method
	 * */
	public boolean addTriggerNameForCheck(String nameForCheck) {

		if (triggerNameList.contains(nameForCheck)) {
			System.out.println("Error - trigger already registered!");
			return false;
		} else {
			triggerNameList.add(nameForCheck);
			return true;
		}
	}

	/**
	 * When an strategy removes itself from a trigger than the trigger
	 * unregisters itself using this method. This is because of the 2 way
	 * dependency.
	 * */
	public boolean removeTriggerNameForCheck(String nameForCheck) {
		if (triggerNameList.remove(nameForCheck)) {
			return true;
		} else {
			System.out.println("Error - trigger is not known, cannot unregister!");
			return false;
		}
	}
}
